package com.android.clinic.doctors.app.ui.patient;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.doctors.app.ClinicDoctorsApplication;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.adapter.BaseListAdapter;
import com.android.clinic.doctors.app.base.BaseRefreshListFragment;
import com.android.clinic.doctors.app.entity.PatientInfo;
import com.android.clinic.doctors.app.glide.ImageLoaderUtils;
import com.android.clinic.doctors.app.http.HttpClient;
import com.android.clinic.doctors.app.http.base.BaseObserver;
import com.android.clinic.doctors.app.http.utils.RxObservableUtils;
import com.android.clinic.doctors.app.ui.widget.UIAlertView;
import com.android.clinic.doctors.app.utils.PermissionUtils;
import com.android.clinic.doctors.app.utils.Utils;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * 已预约
 * Created by cheng on 2018/6/21.
 */
public class ReservedFragment extends BaseRefreshListFragment<PatientInfo> {

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_refresh_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);

    }

    @Override
    protected void reloadData() {
        HttpClient.getHttpService().getListUnCompleted(ClinicDoctorsApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<PatientInfo>>() {
                    @Override
                    public void onSuccess(List<PatientInfo> patientInfo) {
                        refreshComplete();
                        if (patientInfo.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);
                            mAdapter.addAll(patientInfo);
                        } else if (pageNum == 1) {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY, "您还有预约医生，快去预约吧!");
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        refreshComplete();
                        showToast(message);
                    }
                });
    }

    public void postResolved(String id) {
        HttpClient.getHttpService().postResolved(id)
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public void onSuccess(String s) {
                        swipeToLoadLayout.post(() -> swipeToLoadLayout.setRefreshing(true));
                    }

                    @Override
                    public void onFailure(String message) {
                        showToast(message);
                    }
                });
    }

    @Override
    protected BaseListAdapter<PatientInfo> getAdapter() {
        return new ReservedAdapter(mActivity);
    }

    private class ReservedAdapter extends BaseListAdapter<PatientInfo> {

        public ReservedAdapter(Context context) {
            super(context, R.layout.item_patient_list);
        }

        @Override
        protected Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex) {
            return new ViewHolder(convertView);
        }

        @Override
        protected void setupBindViewHolder(PatientInfo object, Object viewHolder, int position, int typeIndex) {
            ViewHolder holder = (ViewHolder) viewHolder;
            if (!Utils.isEmpty(object.getNickName())) {
                holder.tvName.setText(object.getNickName());
            }
            holder.tvPhone.setText(object.getMobile());
            holder.tvTime.setText(Utils.getFormatTime(getItem(position).getVideoTime()));
            if (!Utils.isEmpty(object.getGender())) {
                holder.ivSex.setImageResource(object.getGender().equals("男") ? R.drawable.icon_male : R.drawable.icon_female);
            }
            ImageLoaderUtils.getInstance().loadHeadImage(object.getImgUrl(), holder.ivHead);

            /**
             * 发起视频聊天
             */
            holder.layVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != mOnVideoCallBackListener) {
                        mOnVideoCallBackListener.callBack(object.getId(), object.getUserId(),
                                object.getName(), object.getImgUrl());
                    }
                }
            });

            holder.btnEnd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UIAlertView.alertDialog(getActivity(), "提示",
                            "是否确定结束本次视频问诊", new UIAlertView.OnThemeDialogListener() {
                                @Override
                                public void OnDismissButton() {

                                }
                                @Override
                                public void OnSureButton() {
                                    postResolved(object.getId());
                                }
                            },"是", "否");
                }
            });

            holder.tvPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PermissionUtils.applyCallPhone(getActivity(), new PermissionUtils.CallBack() {
                        @Override
                        public void onGuarantee() {
                            UIAlertView.callPhoneDialog(getActivity(), object.getName() + "的电话", object.getMobile());
                        }

                        @Override
                        public void onDeny() {

                        }
                    });
                }
            });

        }

        class ViewHolder extends BaseViewHolder {

            ImageView ivHead, ivSex;
            TextView tvName, tvTime, tvPhone, tvDefaultName;
            LinearLayout laySendMessage, layVideoParent, layVideo;
            FrameLayout layDefault;
            Button btnEnd;

            public ViewHolder(View itemView) {
                super(itemView);
                ivHead = itemView.findViewById(R.id.iv_head);
                ivSex = itemView.findViewById(R.id.iv_sex);
                tvName = itemView.findViewById(R.id.tv_name);
                tvTime = itemView.findViewById(R.id.tv_time);
                tvPhone = itemView.findViewById(R.id.tv_phone);
                laySendMessage = itemView.findViewById(R.id.layout_send_message);
                layVideoParent = itemView.findViewById(R.id.layout_video_parent);
                layVideo = itemView.findViewById(R.id.layout_video);
                btnEnd = itemView.findViewById(R.id.btn_end);
                layDefault = itemView.findViewById(R.id.layout_default);
                tvDefaultName = itemView.findViewById(R.id.tv_default_name);
                laySendMessage.setVisibility(View.GONE);
                layVideoParent.setVisibility(View.VISIBLE);
            }
        }
    }

    public OnVideoCallBackListener mOnVideoCallBackListener;

    public void setOnVideoCallBackListener(OnVideoCallBackListener mOnVideoCallBackListener) {
        this.mOnVideoCallBackListener = mOnVideoCallBackListener;
    }

    interface OnVideoCallBackListener {
        void callBack(String id, String callId, String name, String imgUrl);
    }
}
